package com.linzelin.www.util;

import com.linzelin.www.po.Activity;
import com.linzelin.www.po.ActivityEnroll;
import com.linzelin.www.po.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Scanner;

import static com.linzelin.www.util.AccountStudent.FindStudent;

public class AccountActivity {
    /*Properties prop = new Properties();*/
    private static JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //先获取一波连接池

    //活动的撤回方法
    public void ActivityBack()
    {
        System.out.println("------------------");
        String sql = "select * from activity where status = '待审核'";
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        if(list.size()==0)
        {
            System.out.println("当前无可撤回的活动");
        }
        else
        {
            System.out.println("当前可撤回的活动有:");
            for (Activity a : list) {
                System.out.println("------------------");
                System.out.println("活动:"+a.getActivity()+"\n活动id:"+a.getId()+"\n活动时长:"+a.getTime()+"\n活动内容:"+a.getContent());
                System.out.println("状态:"+a.getStatus());
            }
            System.out.println("------------------");
            int flag = 0;
            while(flag != -1)
            {
                System.out.println("请输入活动id来选择要撤回的活动,如想退出,请输入-1");
                flag = new Scanner(System.in).nextInt();
                if(FindActivity(flag)==null&&flag!=-1)
                {
                    System.out.println("------------------");
                    System.out.println("无效id,请重新输入");
                }
                else if(flag!=-1)
                {
                    sql = "delete from activity where id = ?";
                    if(template.update(sql,flag) > 0)
                    {
                        System.out.println("------------------");
                        System.out.println("撤回成功");
                    }
                    else
                    {
                        System.out.println("------------------");
                        System.out.println("修改失败");
                    }

                }
            }
        }
    }

    //活动的模糊查询方法
    public void LikeQuery() {
        System.out.println("------------------");
        System.out.println("请输入要查询的活动的关键字");
        System.out.println("------------------");
        String Activityname = new Scanner(System.in).next();
        char[] strs = Activityname.toCharArray();
        //将字符串转化为数组
        String sql = "select * from activity where activity like '%";
        for (int i = 0; i < strs.length; i++) {
            sql = sql + strs[i]+"%";
            if(i==strs.length-1)
            {
                sql = sql + "'";
            }//将模糊搜索转化出来
        }
        sql = sql + "or content like '%";
        for (int i = 0; i < strs.length; i++) {
            sql = sql + strs[i]+"%";
            if(i==strs.length-1)
            {
                sql = sql + "'";
            }//将模糊搜索转化出来
        }
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        if(list.size()==0)
        {
            //当集合长度等于0时代表没有活动可以报名
            System.out.println("------------------");
            System.out.println("当前没有符合条件活动,敬请期待");
        }
        else
        {
            for (Activity aa : list) {
                System.out.println("------------------");
                System.out.println("活动id:"+aa.getId()+"\n活动:"+aa.getActivity());
                System.out.println("报名人数:"+aa.getCount());
                System.out.println("活动简介:"+aa.getContent());
                System.out.println("活动时长:"+aa.getTime());
            }
        }
    }
    /* public boolean login(String activity) {
         if (activity == null) {
             return false;
         }
         String sql = "select * from activity";
         //获取表中符合条件的活动,有就返回true,没有就返回false
         List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
         for (Activity a : list) {
             if(a.getActivity().equals(activity))
             {
                 return true;
             }
         }
         return false;
     }*/
    //活动添加方法
    public void ActivityAdd(Activity activity)  {
        System.out.println("请输入活动内容");
        activity.setStatus("待审核");
        activity.setContent(new Scanner(System.in).next());
        System.out.println("请输入活动时长");
        activity.setTime(new Scanner(System.in).nextInt());
        String sql = "insert into activity values (null,?,?,?,0,?)";
        template.update(sql,activity.getActivity(),activity.getContent(),activity.getStatus(),activity.getTime());
        /*FileWriter fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity.txt",true);
        //创建一个对象用来存放活动
        BufferedWriter bw = new BufferedWriter(fw);
        bw.newLine();
        bw.write(activity.getActivity_name()+"/"+activity.getContent()+"/"+"待审核");
        //将活动以活动名/活动内容/审核状态载入
        bw.flush();
        bw.close();
        fw.close();*/
        System.out.println("------------------");
        System.out.println("活动申报成功，请等待管理员审核");
        /*prop.setProperty(activity.getActivity_name(),activity.getContent());
        FileWriter fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity.txt",true);
        prop.store(fw,"新活动");
        fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity_status.txt",true);
        prop.setProperty(activity.getActivity_name(),"待审核");
        prop.store(fw,"待审核的活动");
        System.out.println("活动申报成功，请等待管理员审核");*/
    }
    //活动审核程序
    public void ActivityExamine(String ACname)//变量是指活动名称
    {
        //活动审核程序,成功的活动就把活动改为审核通过,失败就设置为审核不通过
        if (ACname == null) {
            System.out.println("输入有误");
        }
        String sql = "select * from activity";
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        for (Activity a : list) {
            if(a.getActivity().equals(ACname))
            {
                int count = 0;
                //活动是否修改成功变量
                System.out.println("------------------");
                System.out.println("活动:"+ACname+"  活动时长:"+a.getTime()+"\n活动内容:"+a.getContent());
                System.out.println("审核状态:"+a.getStatus());
                if(a.getStatus().equals("待审核"))
                {
                    System.out.println("请选择是否通过该活动");
                    System.out.println("1.通过  2.不通过");
                    switch(new Scanner(System.in).nextInt())
                    {
                        case 1:
                            a.setStatus("审核通过");
                            sql = "update activity set status = ? where activity = ?";
                            count = template.update(sql,a.getStatus(),ACname);
                            break;
                        case 2:
                            a.setStatus("审核不通过");
                            sql = "update activity set status = ? where activity = ?";
                            count = template.update(sql,a.getStatus(),ACname);
                            break;
                    }
                }
                if(count>0)
                {
                    System.out.println("------------------");
                    System.out.println("修改成功");
                }
                else
                {
                    System.out.println("------------------");
                    System.out.println("修改失败");
                }
                return ;
            }
        }
        //没找到就直接再让用户操作一遍
        System.out.println("未找到活动,请选择输入remove重新寻找或输入任意内容退出");
        if(new Scanner(System.in).next().equals("remove"))
        {
            System.out.println("请重新输入活动名称");
            ActivityExamine(new Scanner(System.in).next());
        }
        /*BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\啦啦啦\\Desktop\\Activity.txt"));
        //创建一个用于修改活动状态的变量,直接覆盖原来的文件
        String line;
        ArrayList<String> txt = new ArrayList<>();
        int i = 0;
        while((line=br.readLine())!=null)
        {
            String[] lines = find_activity(line);
            if(lines[0].equals(ACname))
            {
                i=1;
                System.out.println("活动:"+ACname+"\n"+"活动内容:"+lines[1]);
                System.out.println("审核状态:"+lines[2]);
                if(!(lines[2].equals("审核通过")))
                {
                    System.out.println("请选择是否通过该活动");
                    System.out.println("1.通过  2.不通过");
                    switch(new Scanner(System.in).nextInt())
                    {
                        case 1:lines[2]="审核通过";break;
                        case 2:break;
                    }
                }
                line = ACname+"/"+lines[1]+"/"+lines[2]+"/"+"0";
                //在审核通过状态下,报名人数才设置为0
                //审核没通过,无人报名
                //不用空指针代替是为了防止出现NullPointerException
                //for (int i1 = 1; i1 < lines.length; i1++) {
                    line.concat("/"+lines[i1]);
                }
            }
            txt.add(line);
        }
        if(i==1)
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity.txt"));
            for (String s : txt) {
                bw.write(s);
                bw.newLine();
            }
            bw.flush();
        }
        else
        {
            System.out.println("没有找到你想找到的活动,请重新输入活动名称");
            Activity_enroll(new Scanner(System.in).next());
        }
        br.close();*/
       /* Properties prop_find = new Properties();
        prop_find.load(new FileReader("C:\\Users\\啦啦啦\\Desktop\\Activity_status.txt"));
        Set<String> set = prop_find.stringPropertyNames();
        for (String s : set) {
            if(s.equals(ACname))
            {
                if(prop_find.get(s).equals("审核通过"))
                {
                    System.out.println("报名成功");
                }
            }
        }*/
    }
    //活动报名程序
    public void ActivityEnroll(Student student) {
        System.out.println("------------------");
        System.out.println("请选择活动报名");
        //展示一下有的活动,顺便用list集合将活动保存一下,方便操作
        String sql = "select * from activity where status = '审核通过'";
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        for (Activity aa : list) {
            //遍历下活动
            System.out.println("------------------");
            System.out.println("活动id:"+aa.getId()+"--活动:"+aa.getActivity());
            System.out.println("活动时长:"+aa.getTime()+"--报名人数:"+aa.getCount()+"\n活动简介:"+aa.getContent());
        }
        System.out.println("------------------");
        System.out.println("请输入活动id报名活动");
        int id = new Scanner(System.in).nextInt();
        int flag = 0;
        //用于定义是不是有这个活动id
        for (Activity aa : list) {
            if(aa.getId()==id)
            {
                flag = 1;
                //把flag改为让后面可以继续操作
                if(1==RemoveEnroll(id,student.getId()))
                {
                    System.out.println("重复报名,请等待管理员审核");
                    break;
                }
                else if(RemoveEnroll(id,student.getId())==2)
                {
                    System.out.println("审核通过,请准时参加");
                    break;
                }
                else
                {
                    sql = "insert into activityenroll values(?,?,'待审核')";
                    int count = template.update(sql,id,student.getId());
                    //将活动id和学生id保存到表格中,
                    if(count>0)
                    {
                        System.out.println("报名成功");
                        break;
                    }
                    else
                    {
                        System.out.println("报名失败");
                        break;
                    }
                }
            }
        }
        if(flag==0)
        {
            System.out.println("输入的活动id有误,请重新输入");
            ActivityEnroll(student);
        }

    }
    //用于筛选是不是有重复报名的方法
    public int RemoveEnroll(int aid,int sid)
    { //用于筛选是不是有重复报名的方法
        String sql = "select * from activityenroll";
        List<ActivityEnroll> list = template.query(sql,new BeanPropertyRowMapper<ActivityEnroll>(ActivityEnroll.class));
        for (ActivityEnroll ae : list) {
            if(ae.getStudent_id()==sid)
            {
                if(ae.getActivity_id()==aid)
                {
                    if(ae.getStatus().equals("待审核"))
                    {
                        return 1;
                        //返回1代表重复报名
                    }
                    else if(ae.getStatus().equals("审核通过"))
                    {
                        return 2;
                        //返回2代表审核通过;
                    }
                }
            }
        }
        return 0;
    }

    //审核报名用的
    public void ExamineEnroll()
    {
        String sql = "select * from activityenroll where status = '待审核'";
        DisplayActivity();
        System.out.println("------------------");
        System.out.println("请输入活动id获取该活动的报名人员");
        int aid = new Scanner(System.in).nextInt();
        int flag = 0;
        List<ActivityEnroll> list = template.query(sql,new BeanPropertyRowMapper<ActivityEnroll>(ActivityEnroll.class));
        //读取当前在审核的活动报名
        String FA=FindActivity(aid);
        if(FA!=null)
        {
            flag=2;
            System.out.println("活动:"+FA);
            for (ActivityEnroll ae : list) {
                if(ae.getActivity_id()==aid)
                {
                    flag = 1;
                    System.out.println("学生"+FindStudent(ae.getStudent_id())+"--学生id:"+ae.getStudent_id());
                }
            }
        }
        if(flag==2)
        {
            System.out.println("当前活动无人报名,请重新选择活动");
            System.out.println("------------------");
            ExamineEnroll();
        }
        else if(flag==0)
        {
            System.out.println("无该活动,请重新选择活动id");
            System.out.println("------------------");
            ExamineEnroll();
        }
        //当导标等于1是进入循环,让负责人可以重复操作审核
        while(flag==1)
        {
            System.out.println("------------------");
            System.out.println("请输入学生id,选择要进行的服务,如果想退出请输入 -1");
            int sid = new Scanner(System.in).nextInt();
            if(FindStudent(sid)!=null)
            {
                sql = "update activityenroll set status = ? where student_id = ? and activity_id = ?";
                System.out.println("------------------");
                System.out.println("请选择服务:\n1.通过\n2.不通过\n3.退出");
                switch(new Scanner(System.in).nextInt())
                {
                    case 1:
                        flag = template.update(sql,"审核通过",sid,aid);
                        sql = "update activity set count = count+1 where id = "+aid+"";
                        template.update(sql);
                    break;
                    case 2:flag = template.update(sql,"审核不通过",sid,aid);break;
                    case 3:flag = -1;
                        System.out.println("正在退出");break;
                }
                if(flag==1)
                {
                    System.out.println("修改成功");
                }
                else
                {
                    System.out.println("修改失败");
                }

            }
            else if(sid == -1)
            {
                //负责人不想审核了就靠这个退出
                flag = -1;
                System.out.println("正在退出");
            }
            else
            {
                System.out.println("无效id,请重新输入");
            }
        }
    }

    //用于通过id来获取活动名
    public static String FindActivity(int aid)
    {
        String sql = "select * from activity where id ="+aid+"";
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        for (Activity activity : list) {
            if(activity.getId() == aid)
            {
                return activity.getActivity();
            }
        }
        return null;
    }


    //用于展示已经有的活动的
    public void DisplayActivity()
    {
        String sql = "select * from activity where status = '审核通过'";
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        if(list.size()==0)
        {
            //当集合长度等于0时代表没有活动可以报名
            System.out.println("------------------");
            System.out.println("当前没有活动可以报名,敬请期待");
            return;
        }
        for (Activity aa : list) {
            System.out.println("------------------");
            System.out.println("活动id:"+aa.getId()+"--活动:"+aa.getActivity());
            System.out.println("报名人数:"+aa.getCount());
        }
    }
    /*public String[] find_activity(String line)
    {
        return line.split("/");
    }*/

    //结束活动用函数
    public void EndAcitivty()
    {
        DisplayActivity();
        //先展示一下有多少活动
        System.out.println("------------------");
        System.out.println("请选择活动id,如想退出,请输入-1");
        int aid = new Scanner(System.in).nextInt();
        if(aid==-1)
        {
            System.out.println("------------------");
            System.out.println("正在退出");
        }
        else {
            String FA = FindActivity(aid);
            while (FA == null) {
                System.out.println("id输入有误,请重新输入");
                aid = new Scanner(System.in).nextInt();
                FA = FindActivity(aid);
            }//还是要筛选一下的,万一输错id了呢?
            int flag = 0;
            //经典flag
            System.out.println("------------------");
            System.out.println("请选择是否要结束活动\n1.结束 \n2.返回");
            switch (new Scanner(System.in).nextInt())
            {
                case 1:
                    String sql = "select * from activity where id = "+aid+"";
                    if(true)
                    {
                        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
                        if (list.size()==1)
                        {
                            DurationAdd(aid,list.get(0).getTime());
                        }
                        else
                        {
                            System.out.println("系统出错,正在退出");
                            return;
                        }//这里是赋予时长,用if套起来是为 了不浪费内存,让list用了就直接被施放
                    }
                    sql = "delete from activity where id = ?";
                    template.update(sql,aid);
                    //删除活动;
                    sql = "delete from activityenroll where activity_id = ?";
                    //删除中间表里的活动
                    template.update(sql,aid);
            }
        }

    }
    //输入活动aid结算student类里面的活动时长,还要输入活动的时长
    public void DurationAdd(int aid,int duration)
    {

        String sql = "select * from activityenroll where activity_id = "+aid+"";
        List<ActivityEnroll> list = template.query(sql,new BeanPropertyRowMapper<ActivityEnroll>(ActivityEnroll.class));
        //获取list集合来获取学生id
        for (ActivityEnroll ae : list) {
            sql = "update student set duration = duration + ? where id = ?";
            template.update(sql,duration,ae.getStudent_id());
            //更新学生的活动时长;
        }
    }

}
